package com.emep.changzhi.analyse.service.outlet;

import com.emep.changzhi.analyse.entity.basics.SysAreaInfo;
import com.emep.changzhi.analyse.entity.basics.SysCompanyInfo;
import com.emep.changzhi.analyse.entity.outlet.SysOutletInfo;
import com.emep.changzhi.analyse.repository.basics.SysAreaInfoRepository;
import com.emep.changzhi.analyse.repository.basics.SysCompanyInfoRepository;
import com.emep.changzhi.analyse.repository.outlet.SysOutletInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 排口信息业务层
 * @date 2018/2/26 14:50
 */
@Service
public class SysOutletInfoService {
    
    @Autowired
    private SysOutletInfoRepository sysOutletInfoRepository;

    @Autowired
    private SysCompanyInfoRepository sysCompanyInfoRepository;
    @Autowired
    SysAreaInfoRepository sysAreaInfoRepository;

    private static final Logger logger = LoggerFactory.getLogger(SysOutletInfoService.class);


    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysOutletInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return  sysOutletInfoRepository.findAll(pageable);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增排口信息
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysOutletInfo save(Map<String, Object> payload) {
        SysOutletInfo SysOutletInfo = new SysOutletInfo();
        try {
            SysOutletInfo SysOutletInfo1 = (SysOutletInfo) MapUtils.convertMap(SysOutletInfo.class,payload);
            SysOutletInfo1.setCompany(sysCompanyInfoRepository.findSysCompanyInfoById(payload.get("companyId").toString()));
            SysOutletInfo = sysOutletInfoRepository.save(SysOutletInfo1);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return SysOutletInfo;
    }


    /**
     *
     * 根据id查询排口信息
     *
     * @param id
     * @return
     */
    public SysOutletInfo findId(String id){
        SysOutletInfo SysOutletInfo = null;
        try {
            SysOutletInfo = sysOutletInfoRepository.findSysOutletInfoById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return SysOutletInfo;
    }

    /**
     *
     * 根据id 删除排口信息
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysOutletInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有排口信息
     *
     * @return List<SysOutletInfo>
     */
    public List<SysOutletInfo> findAll() {
        List<SysOutletInfo> SysOutletInfos = null;
        try {
            SysOutletInfos =  sysOutletInfoRepository.findAll();
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return SysOutletInfos;
    }

    /**
     * 分页查询各种状态的排口
     */
    public List<SysOutletInfo> findByOutletStatus(SysOutletInfo sysOutletInfo, String areaCode){
        try {
            SysAreaInfo sysAreaInfo = sysAreaInfoRepository.findSysAreaInfoByAreaCode(areaCode);
            List<SysCompanyInfo> sysCompanyInfos = sysCompanyInfoRepository.findSysCompanyInfosBySysAreaInfos(sysAreaInfo);
            List<SysOutletInfo> sysOutletInfos = new ArrayList<>();
            for(int i = 0 ;i < sysCompanyInfos.size();i++){
                sysOutletInfos.addAll(sysOutletInfoRepository.findByOutletStatusAndCompany(sysOutletInfo.getOutletStatus(),sysCompanyInfos.get(i)));
            }
            return sysOutletInfos;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }
    /**
     * 获取企业排口的情况
     */
    public List<SysOutletInfo> findSysOutletInfosByCompany(SysCompanyInfo sysCompanyInfo){
        try {
            List<SysOutletInfo> sysOutletInfos = sysOutletInfoRepository.findSysOutletInfosByCompany(sysCompanyInfo);
            return sysOutletInfos;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }

    public List<SysOutletInfo> findAllByCompanyAndId(SysCompanyInfo sysCompanyInfo,String id){
        try {
            List<SysOutletInfo> sysOutletInfos = sysOutletInfoRepository.findAllByCompanyAndId(sysCompanyInfo,id);
            return sysOutletInfos;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }

}
